package com.pkdl.dao;

import com.pkdl.dto.ChiTietChamCongDTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

// Data access object, which get data of ChiTietChamCong table from database
public class ChiTietChamCongDAO {
	public ArrayList<ChiTietChamCongDTO> selectAll() {
		ArrayList<ChiTietChamCongDTO> list = new ArrayList<ChiTietChamCongDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_ChiTietChamCong_SelectAll()}");
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				ChiTietChamCongDTO chitietchamcong = new ChiTietChamCongDTO();
				chitietchamcong.setMaCTCC(rs.getInt("MaCTCC"));
				chitietchamcong.setMaLuong(rs.getInt("MaLuong"));
				chitietchamcong.setNgay(rs.getInt("Ngay"));
				chitietchamcong.setCoDiLam(rs.getBoolean("CoDiLam"));
				list.add(chitietchamcong);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ChiTietChamCongDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ChiTietChamCongDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public ChiTietChamCongDTO selectById(int mactcc) {
		ChiTietChamCongDTO chitietchamcong = new ChiTietChamCongDTO();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_ChiTietChamCong_SelectByID(?)}");
			cs.setInt(1, mactcc);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				chitietchamcong.setMaCTCC(rs.getInt("MaCTCC"));
				chitietchamcong.setMaLuong(rs.getInt("MaLuong"));
				chitietchamcong.setNgay(rs.getInt("Ngay"));
				chitietchamcong.setCoDiLam(rs.getBoolean("CoDiLam"));
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ChiTietChamCongDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ChiTietChamCongDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return chitietchamcong;
	}

	public boolean insert(ChiTietChamCongDTO chitietchamcong) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_ChiTietChamCong_Insert(?, ?, ?)}");
			cs.setInt(1, chitietchamcong.getMaLuong());
			cs.setInt(2, chitietchamcong.getNgay());
			cs.setBoolean(3, chitietchamcong.getCoDiLam());
			int n = cs.executeUpdate();
			if (n == 0) {
				result =false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ChiTietChamCongDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ChiTietChamCongDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public boolean update(ChiTietChamCongDTO chitietchamcong) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_ChiTietChamCong_Update(?, ?, ?, ?)}");
			cs.setInt(1, chitietchamcong.getMaCTCC());
			cs.setInt(2, chitietchamcong.getMaLuong());
			cs.setInt(3, chitietchamcong.getNgay());
			cs.setBoolean(4, chitietchamcong.getCoDiLam());
			int n = cs.executeUpdate();
			if (n == 0) {
				result =false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ChiTietChamCongDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ChiTietChamCongDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public boolean delete(int mactcc) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_ChiTietChamCong_Delete(?)}");
			cs.setInt(1, mactcc);
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ChiTietChamCongDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ChiTietChamCongDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public ArrayList<ChiTietChamCongDTO> selectByMaLuong(int maluong) {
		ArrayList<ChiTietChamCongDTO> list = new ArrayList<ChiTietChamCongDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_ChiTietChamCong_SelectByMaLuong(?)}");
			cs.setInt(1, maluong);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				ChiTietChamCongDTO chitietchamcong = new ChiTietChamCongDTO();
				chitietchamcong.setMaCTCC(rs.getInt("MaCTCC"));
				chitietchamcong.setMaLuong(rs.getInt("MaLuong"));
				chitietchamcong.setNgay(rs.getInt("Ngay"));
				chitietchamcong.setCoDiLam(rs.getBoolean("CoDiLam"));
				list.add(chitietchamcong);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ChiTietChamCongDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ChiTietChamCongDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return list;
	}

}
